package io.renren.modules.seal.utils;

import com.tcl.jr.eap.VirtualFile;
import com.tcl.jr.eap.VirtualFolder;
import com.tcl.jr.eap.ssapi.impl.BatchSubmitTrade;
import com.tcl.jr.eap.ssapi.impl.EAPTradeServiceImpl;
import com.tcl.jr.eap.ssapi.util.StringUtil;
import com.tcl.jr.ssapi.EAPTradeService;
import com.tcl.jr.ssapi.EapException;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;


/**
 * @author xiegaolong
 * @description EAP电子档案工具类
 * @date 2021/5/11
 */
@Slf4j
public class HXEAPUtils {
    private static String SEPARATOR = "/";
    private HXEAPUtils() {
        //EAPUtils
    }

    /**
     * 应用号
     */
    private static final String HXAPPNO = PropertiesUtils.getString("hxeap.appno");
    /**
     * 文件类型
     */
    private static final String HXBUSI_TYPE = PropertiesUtils.getInt("hxeap.busiType") + "";
    /**
     * 下载文件临时存放目录
     */
    private static final String HXTEMP_PATH = PropertiesUtils.getString("hxeap.tempPath");

    private static final EAPTradeService tradeService = new EAPTradeServiceImpl();

    /**
     * EAP上传文件
     *
     * @param fileName
     * @param content
     * @return 电子档案编码
     */
    public static EapFile uploadFile(String fileName, byte[] content) {
        try {
            VirtualFolder vFiles = new VirtualFolder("src-folder");
            // 正常图片
            VirtualFile vFile = new VirtualFile(fileName);
            vFile.setContent(content);
            vFiles.addFile(vFile);
            String busiid = save2Eap(vFiles);
            if (StringUtil.isNotNull(busiid) && busiid.length() == 32) {
                return new EapFile(busiid, fileName);
            }
        } catch (Exception e) {
            log.error("上传EAP电子档案错误，请检查文件 Cause by: " + e.getCause());
            e.printStackTrace();
        }
        return null;
    }

    private static String save2Eap(VirtualFolder folder) throws EapException {
        // 调用eap上传文件
        BatchSubmitTrade submit = new BatchSubmitTrade();
        // "a;b;c;d;e;f;g;h;i;j;k;l;m;n", "1", "0", "0", "admin" 参数是根据底层jar包分析硬编码固定传入
        return submit.execute(folder, HXAPPNO, "00000", HXBUSI_TYPE,
                "a;b;c;d;e;f;g;h;i;j;k;l;m;n", "1", "0", "0", "admin");
    }

    /**
     * 下载单个文件
     */
    public static String downloadFile(EapFile eapFile) {
        String basePath = HXTEMP_PATH + SEPARATOR + DateUtils.dateTimeNow("yyyyMMdd");
        String folderName = DateUtils.dateTimeNow("yyyyMMddHHmmss");
        String dirPath = basePath + SEPARATOR + folderName;
        boolean success = download(eapFile, dirPath);
        if (success) {
            return dirPath + SEPARATOR + eapFile.getFilename();
        }
        return null;
    }


    private static boolean download(EapFile fileInfo, String dirPath) {
        String result = tradeService.getBusiid(fileInfo.getBusiid(), dirPath, fileInfo.getFilename());
        if (StringUtil.isNotNull(result)) {
            return result.contains("1000");
        }
        return false;
    }

    public static void remove(String... busiids) {
        for (String busiid : busiids) {
            tradeService.deleteBusiid(busiid);
        }
    }

    public static byte[] file2Byte(File file) {
        byte[] data = null;

        try (FileInputStream fis = new FileInputStream(file);
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            data = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 将文件转换成byte数组
     *
     * @param tradeFile
     * @return
     */
    public static byte[] File2byte(File tradeFile) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
